/*
 * Copyright 2013 HVTTGROUP
 * Tool Genneric code by hvttgroup
 * @version v1.0
 * @see http://hvttgroup.com
 *
 */
package com.hvtt.web.models;

import com.hvtt.web.connection.Command;
import com.hvtt.web.connection.WEBConnection;
import com.hvtt.web.connection.WEBParamCollection;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

/**
 *
 * @author Nguyen Hoang Danh
 */
public class Languagesdef implements Serializable{

// <editor-fold defaultstate="collapsed" desc="Generic code properties for class">
    private String languagesDefID = "";
    private String languagesID = "";
    private String content = "";
    private WEBConnection webConnection;
    private static String STORE_NAME = "{call proc_languagesdef(?,?,?,?)}";
    private static String TABLE_NAME = "languagesdef";
    private static final String key = "LanguagesDefID";
    private static final String key1 = "LanguagesID";

//</editor-fold>
// <editor-fold defaultstate="collapsed" desc="Generic code Constructor class">
    public Languagesdef() {
        webConnection = WEBConnection.getInstance();
    }

    public Languagesdef(String languagesDefID, String languagesID) {
        this.languagesDefID = languagesDefID;
        this.languagesID = languagesID;
        webConnection = WEBConnection.getInstance();
    }

    public Languagesdef(String languagesDefID, String languagesID, String content) {
        this.languagesDefID = languagesDefID;
        this.languagesID = languagesID;
        this.content = content;
        webConnection = WEBConnection.getInstance();
    }

//</editor-fold>
// <editor-fold defaultstate="collapsed" desc="Generic code init param">
    private WEBParamCollection fillParamsAndAction(String action) {
        WEBParamCollection params = WEBParamCollection.getInstance();
        params.add(action);
        params.add(this.getLanguagesDefID());
        params.add(this.getLanguagesID());
        params.add(this.getContent());
        return params;
    }

    private WEBParamCollection fillParamsAndAction(String action, Languagesdef languagesdef1) {
        WEBParamCollection params = WEBParamCollection.getInstance();
        params.add(action);
        params.add(languagesdef1.getLanguagesDefID());
        params.add(languagesdef1.getLanguagesID());
        params.add(languagesdef1.getContent());
        return params;
    }

// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Generic code event">
    public boolean insert(Languagesdef varLanguagesdef) {
        boolean bExcute = webConnection.executeUpdate(STORE_NAME, Command.sqlStoredProcedure, fillParamsAndAction("Add", varLanguagesdef));
        return bExcute;
    }

    public boolean save() {
        boolean bExcute = webConnection.executeUpdate(STORE_NAME, Command.sqlStoredProcedure, fillParamsAndAction("Add"));
        return bExcute;
    }

    public boolean update(Languagesdef varLanguagesdef) {
        boolean bExcute = webConnection.executeUpdate(STORE_NAME, Command.sqlStoredProcedure, fillParamsAndAction("Update", varLanguagesdef));
        return bExcute;
    }

    public boolean update() {
        boolean bExcute = webConnection.executeUpdate(STORE_NAME, Command.sqlStoredProcedure, fillParamsAndAction("Update"));
        return bExcute;
    }

    public boolean remove(Languagesdef languagesdef1) {
        this.setLanguagesDefID(languagesdef1.getLanguagesDefID());
        this.setLanguagesID(languagesdef1.getLanguagesID());
        boolean bExcute = webConnection.executeUpdate(STORE_NAME, Command.sqlStoredProcedure, fillParamsAndAction("Delete"));
        return bExcute;
    }

    public boolean remove(List<Languagesdef> listLanguagesdef) {
        WEBParamCollection p = WEBParamCollection.getInstance();
        boolean result = false;
        String sql = "delete from " + TABLE_NAME + " where ";
        for (int i = 0; i < listLanguagesdef.size(); i++) {
            sql = sql + "(" + TABLE_NAME + "." + key + " = " + "? and " + TABLE_NAME + "." + key1 + " = ?) or ";
            p.add(listLanguagesdef.get(i).getLanguagesDefID());
            p.add(listLanguagesdef.get(i).getLanguagesID());
            if (((i + 1) % 900) == 0) {
                sql = sql.substring(0, sql.lastIndexOf("or"));
                result = webConnection.executeUpdate(sql, Command.sqlText, p);
                p.clear();
                sql = "delete from " + TABLE_NAME + " where ";
            }
        }
        if (sql.lastIndexOf("or") != -1) {
            sql = sql.substring(0, sql.lastIndexOf("or"));
            result = webConnection.executeUpdate(sql, Command.sqlText, p);
        }
        return result;
    }

    public boolean removeAll() {
        WEBParamCollection p = WEBParamCollection.getInstance();
        boolean bExcute = webConnection.executeUpdate("delete from " + TABLE_NAME, Command.sqlText, p);
        return bExcute;
    }

    public Languagesdef findByKey(Languagesdef languagesdef) {
        Languagesdef varLanguagesdef1 = null;

        try {
            this.setLanguagesDefID(languagesdef.getLanguagesDefID());
            this.setLanguagesID(languagesdef.getLanguagesID());
            List<Languagesdef> result = webConnection.executeQuery(STORE_NAME, Command.sqlStoredProcedure, fillParamsAndAction("GetByKey"), Languagesdef.class);
            if (!result.isEmpty()) {
                varLanguagesdef1 = result.get(0);
            }
        } catch (SQLException ex) {
            Logger.getLogger(Languagesdef.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
        }
        return varLanguagesdef1;
    }

    public List<Languagesdef> findAll() {
        List<Languagesdef> lstLanguagesdef = null;
        try {
            lstLanguagesdef = webConnection.executeQuery(STORE_NAME, Command.sqlStoredProcedure, fillParamsAndAction("GetAll"), Languagesdef.class);
        } catch (SQLException ex) {
            Logger.getLogger(Languagesdef.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
        }
        return lstLanguagesdef == null ? new ArrayList<Languagesdef>() : lstLanguagesdef;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="Generic code getter and setter for properties of class">
    public String getLanguagesDefID() {
        return languagesDefID;
    }

    public String getLanguagesID() {
        return languagesID;
    }

    public String getContent() {
        return content;
    }

    public void setLanguagesDefID(String languagesDefID) {
        this.languagesDefID = languagesDefID;
    }

    public void setLanguagesID(String languagesID) {
        this.languagesID = languagesID;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public void resetAttribute() {
        languagesDefID = "";
        languagesID = "";
        content = "";
    }

    public void setWebConnection(WEBConnection webConnection) {
        this.webConnection = webConnection;
    }

    public WEBConnection getWebConnection() {
        return webConnection;
    }

//</editor-fold>
// <editor-fold defaultstate="collapsed" desc="Generic code hasCode and equals method">
    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(this.languagesDefID).append(this.languagesID).toHashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj instanceof Languagesdef) {
            Languagesdef otherObj = (Languagesdef) obj;
            return new EqualsBuilder().append(this.languagesDefID, otherObj.languagesDefID).append(this.languagesID, otherObj.languagesID).isEquals();
        }
        return false;
    }

//</editor-fold>
// <editor-fold defaultstate="collapsed" desc="Generic code filterFields">
    public static Field[] filterFields() {
        try {
            Class c = Languagesdef.class;
            return new Field[]{c.getDeclaredField("languagesDefID"), c.getDeclaredField("languagesID"), c.getDeclaredField("content")};
        } catch (NoSuchFieldException | SecurityException ex) {
            Logger.getLogger(Languagesdef.class.getName()).log(Level.SEVERE, null, ex);
        }
        return new Field[]{};
    }
//</editor-fold>
// <editor-fold defaultstate="collapsed" desc="Generic code other method">

    public Languagesdef newInstance() {
        try {
            return this.getClass().newInstance();
        } catch (InstantiationException | IllegalAccessException ex) {
            Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
        }
        return new Languagesdef();
    }

    /**
     * Copy properties form Languagesdef to this
     *
     * @param languagesdef1
     * @return Languagesdef
     */
    public Languagesdef newInstance(Languagesdef languagesdef1) {
        this.setLanguagesDefID(languagesdef1.getLanguagesDefID());
        this.setLanguagesID(languagesdef1.getLanguagesID());
        this.setContent(languagesdef1.getContent());
        return this;
    }
//</editor-fold>
}
